<template>
  <div class="app-container">
    <el-row :gutter="20" class="mb8" v-if="deptId == 101">
      <el-col :span="2">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增公告</el-button
        >
      </el-col>
      <el-col :span="2">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="annouce"
          >发布公告</el-button
        >
      </el-col>
      <el-col :span="2">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除公告</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="announcementList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="deptId === 101"
        :key="Math.random()"
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column label="紧急程度" align="center" prop="emergencyLevel">
        <template slot-scope="scope">
          <div v-if="scope.row.emergencyLevel == '1'">低</div>
          <div v-if="scope.row.emergencyLevel == '2'">中</div>
          <div v-if="scope.row.emergencyLevel == '3'">高</div>
        </template>
      </el-table-column>
      <el-table-column label="公告编码" align="center" prop="code" />
      <el-table-column
        v-if="deptId == 101"
        label="公告状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.status == '1'">草稿</div>
          <div v-if="scope.row.status == '2'">已发布</div>
          <div v-if="scope.row.status == '3'">已删除</div>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
        prop="releaseTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公告主题" align="center" prop="topic" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetial(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公告编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入公告编码" />
        </el-form-item>
        <el-form-item label="紧急程度" prop="emergencyLevel">
          <el-select v-model="form.emergencyLevel" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公告主题" prop="topic">
          <el-input v-model="form.topic" placeholder="请输入公告主题" />
        </el-form-item>
        <el-form-item label="公告内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="适用供应商">
          <el-select
            v-model="form.supplierIds"
            multiple
            placeholder="请选择适用供应商"
          >
            <el-option
              v-for="item in supplierOptions"
              :key="item.supplierId"
              :label="item.supplierName"
              :value="item.supplierId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情 -->
    <el-drawer title="公告详情" :visible.sync="drawer" :with-header="false">
      <div :v-model="drawerForm">
        <div class="topic">主题：{{ drawerForm.topic }}</div>
        <div class="drawerTop">
          <div class="code">公告编号：{{ drawerForm.code }}</div>
          <div class="level" v-if="drawerForm.emergencyLevel == '1'">
            紧急程度：低
          </div>
          <div class="level" v-if="drawerForm.emergencyLevel == '2'">
            紧急程度：中
          </div>
          <div class="level" v-if="drawerForm.emergencyLevel == '3'">
            紧急程度：高
          </div>
        </div>
        <div class="supplierNames" v-if="deptId == 101">
          适用供应商：{{ drawerForm.supplierNames }}
        </div>
        <div class="content">内容：{{ drawerForm.content }}</div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  listSupplierAnnouncement,
  listEnterpriseAnnouncement,
  getAnnouncement,
  deleteByIds,
  addAnnouncement,
  updateAnnouncement,
  updateByIds,
} from "@/api/system/announcement";

import { listSupplier } from "@/api/system/enterprise";
import { getUserProfile } from "@/api/system/user";

export default {
  name: "Announcement",
  data() {
    return {
      deptId: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      announcementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options: [
        {
          value: "1",
          label: "低",
        },
        {
          value: "2",
          label: "中",
        },
        {
          value: "3",
          label: "高",
        },
      ],
      supplierOptions: [],
      drawer: false,
      drawerForm: {},
    };
  },
  created() {
    this.getUserInfo();
  },
  methods: {
    /** 获取我的用户信息 */
    getUserInfo() {
      getUserProfile().then((response) => {
        this.deptId = response.data.deptId;
        this.getList();
      });
    },
    /** 查看详情 */
    handleDetial(row) {
      getAnnouncement(row.id).then((response) => {
        this.drawerForm = response.data;
        this.drawer = true;
      });
    },
    /** 发布公告 */
    annouce() {
      let obj = {
        ids: this.ids,
      };
      updateByIds(obj).then((response) => {
        if (response.code == 200) {
          this.$message({
            message: "发布成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
    /** 获取我的供应商 */
    getMySuppliers() {
      listSupplier().then((response) => {
        this.supplierOptions = response.rows;
      });
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      if (this.deptId == 101) {
        listEnterpriseAnnouncement(this.queryParams).then((response) => {
          this.announcementList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        listSupplierAnnouncement(this.queryParams).then((response) => {
          this.announcementList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        emergencyLevel: null,
        code: null,
        status: "0",
        releaseTime: null,
        topic: null,
        content: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length === 0;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
      this.getMySuppliers();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAnnouncement(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAnnouncement(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除公告操作 */
    handleDelete() {
      let obj = {
        ids: this.ids,
      };
      deleteByIds(obj).then((response) => {
        if (response.code == 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.getList();
        }
      });
    },
  },
};
</script>

<style>
.drawerTop {
  display: flex;
  padding-top: 50px;
}
.code {
  padding-left: 60px;
}
.level {
  padding-left: 50px;
}
.supplierNames,
.topic,
.content {
  padding-top: 50px;
  text-align: center;
}
.topic {
  font-weight: bold;
}
</style>
